import scrapy
from scrapy import Request
from scrapy.http import HtmlResponse
from taobaoSpider.items import TaobaospiderItem


class TaobaoSpider(scrapy.Spider):
    name = "taobao"
    allowed_domains = ["taobao.com"]
    # start_urls = ["https://taobao.com"]
    def start_requests(self):
        keywords=['手机','笔记本电脑','键鼠套装']
        keywords=['手机']
        for keyword in keywords:
            for page in range(1):
                url=f'https://s.taobao.com/search?q={keyword}&s={page*48}'
                yield Request(url)

    def parse(self, response:HtmlResponse):
        # print(response.text)
        cssitem_list=response.xpath('//*[@id="root"]/div/div[3]/div[1]/div[1]/div[2]/div[3]/div/div')
        # print(len(cssitem_list))
        for cssitem in cssitem_list:
            item=TaobaospiderItem()
            item['title']=cssitem.xpath('./a/div/div[1]/div[2]/div/span/text()').extract()
            yield item
        pass
